//
//  NYHomeTableSectionView.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/4.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYHomeTableSectionView.h"

static NSInteger const ButtonTag = 8900;

@interface NYHomeTableSectionView ()

@property (nonatomic, weak) id<NYHomeTableSectionDelegate>delegate;
@property (nonatomic, assign) NYHomeEntryStatus entryStatus;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) QMUIButton *excellentButton;
@property (nonatomic, strong) QMUIButton *theNewButton;
@property (nonatomic, strong) UIView *followView;

@end

@implementation NYHomeTableSectionView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
                               delegate:(id<NYHomeTableSectionDelegate>)delegate withEntryStatus:(NYHomeEntryStatus)status {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.delegate = delegate;
        self.entryStatus = status;
        [self initUI];
    }
    return self;
}

#pragma mark - Intial Methods
- (void)initUI {
    
    self.backgroundColor = UIColorForBackground;
    
    [self.contentView addSubview:self.bottomView];
    [_bottomView addSubview:self.excellentButton];
    [_bottomView addSubview:self.theNewButton];
    [self.contentView addSubview:self.followView];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.top.height.equalTo(self.contentView);
    }];
    [self.excellentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.bottom.equalTo(self.contentView);
        make.width.mas_equalTo(88);
    }];
    
    [self.theNewButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(self.contentView);
        make.left.equalTo(self.excellentButton.mas_right);
        make.width.mas_equalTo(88);
    }];

    [self.followView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo((self.entryStatus == NYHomeEntryExcellent) ? self.excellentButton.mas_centerX : self.theNewButton.mas_centerX);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(56);
        make.bottom.equalTo(self.contentView);
    }];
    _excellentButton.selected = (self.entryStatus == NYHomeEntryExcellent) ? true : false;
    _theNewButton.selected = (self.entryStatus == NYHomeEntryExcellent) ? false : true;
}
#pragma mark - Target Methods
- (void)selectTheCourseEvent:(QMUIButton *)sender {
    
    if (sender.centerX == _followView.centerX) return;
    
    NSInteger index = sender.tag - ButtonTag;
    NYHomeEntryStatus status = (index == 1) ? NYHomeEntryExcellent : NYHomeEntryNew;
    if ([self.delegate respondsToSelector:@selector(selectTheNYHomeEntryStatus:)]) {
        [self.delegate selectTheNYHomeEntryStatus:status];
    }
}
#pragma mark - Public Methods

#pragma mark - Private Method

#pragma mark - External Delegate

#pragma mark - Setter Getter Methods
- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = UIColorHex(0xffffff);
    }
    return _bottomView;
}
- (QMUIButton *)excellentButton {
    if (!_excellentButton) {
        _excellentButton = [QMUIButton buttonWithType:UIButtonTypeCustom];
        _excellentButton.backgroundColor = [UIColor clearColor];
        [_excellentButton setTitle:@"精品课程" forState:0];
        _excellentButton.titleLabel.font = UIFontMake(14);
        [_excellentButton addTarget:self action:@selector(selectTheCourseEvent:) forControlEvents:UIControlEventTouchUpInside];
        [_excellentButton setTitleColor:UIColorMakeWithHex(@"#333333") forState:0];
        [_excellentButton setTitleColor:UIColorMakeWithHex(@"#5396FF") forState:UIControlStateSelected];
        _excellentButton.tag = ButtonTag + 1;
        _excellentButton.selected = true;
    }
    return _excellentButton;
}
- (QMUIButton *)theNewButton {
    if (!_theNewButton) {
        _theNewButton = [QMUIButton buttonWithType:UIButtonTypeCustom];
        _theNewButton.backgroundColor = [UIColor clearColor];
        [_theNewButton setTitle:@"最新课程" forState:0];
        _theNewButton.titleLabel.font = UIFontMake(14);
        [_theNewButton addTarget:self action:@selector(selectTheCourseEvent:) forControlEvents:UIControlEventTouchUpInside];
        [_theNewButton setTitleColor:UIColorMakeWithHex(@"#333333") forState:0];
        [_theNewButton setTitleColor:UIColorMakeWithHex(@"#5396FF") forState:UIControlStateSelected];
        _theNewButton.tag = ButtonTag + 2;
        _theNewButton.selected = false;
    }
    return _theNewButton;
}
- (UIView *)followView {
    if (!_followView) {
        _followView = [[UIView alloc] init];
        _followView.backgroundColor = UIColorMakeWithHex(@"#5396FF");
    }
    return _followView;
}

@end
